const tagsView = {
    state: {
        visitedViews: [],
        cachedViews: []
    },
    mutations: {
        ADD_VISITED_VIEWS: (state, view) => {
            // if (view.meta.noCache) {
            //     return false;
            // }
            // state.cachedViews.push(view.name)
            if (state.visitedViews.some(v => v.name === view.name)) return
            state.visitedViews.push({
                name: view.name,
                path: view.path,
                fullPath: view.fullPath,
                title: view.meta.title || 'no-name'
            })
        },
        ADD_CACHED_VIEW: (state, view) => {
            // console.log(view.name)
            // console.log(state.cachedViews(view.name))
            if (state.cachedViews.includes(view.name))return
            if (!view.meta.noCache) {
                state.cachedViews.push(view.name)
            }
        },
        DEL_CACHED_VIEW: (state, view) => {
            for (const i of state.cachedViews) {
                if (i === view.name) {
                    const index = state.cachedViews.indexOf(i)
                    state.cachedViews.splice(index, 1)
                    // console.log(state.cachedViews[0])
                    break;
                }
            }
        },
        DEL_OTHERS_CACHED_VIEWS: (state, view) => {
            for (const i of state.cachedViews) {
              if (i === view.name) {
                const index = state.cachedViews.indexOf(i)
                state.cachedViews = state.cachedViews.slice(index, index + 1)
                break
              }
            }
          },
          DEL_ALL_CACHED_VIEWS: state => {
            state.cachedViews = []
          },
        DEL_VISITED_VIEWS: (state, view) => {
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.path === view.path) {
                    state.visitedViews.splice(i, 1)
                    break
                }
            }
            for (const i of state.cachedViews) {
                if (i === view.name) {
                    const index = state.cachedViews.indexOf(i)
                    state.cachedViews.splice(index, 1)
                    break
                }
            }
        },
        DEL_OTHERS_VIEWS: (state, view) => {
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.path === view.path) {
                    state.visitedViews = state.visitedViews.slice(i, i + 1)
                    break
                }
            }
            for (const i of state.cachedViews) {
                if (i === view.name) {
                    const index = state.cachedViews.indexOf(i)
                    state.cachedViews = state.cachedViews.slice(index, i + 1)
                    break
                }
            }
        },
        DEL_ALL_VIEWS: (state) => {
            state.visitedViews = []
            state.cachedViews = []
        }
    },
    actions: {
        //编辑标签标题,$nextTick中调用
        editTageTitle({commit, state}, data) {
            let item=state.visitedViews.find(x=>x.fullPath===data.fullPath||x.path===data.path||x.name===data.name);
            if(item){
              item.title=data.title;
            }
        },
        addVisitedViews({commit}, view) {
            commit('ADD_VISITED_VIEWS', view)
            commit('ADD_CACHED_VIEW', view)
        },
        // delView({ dispatch, state }, view) {
        // return new Promise(resolve => {
        //     dispatch('delVisitedView', view)
        //     dispatch('delCachedView', view)
        //     resolve({
        //     visitedViews: [...state.visitedViews],
        //     cachedViews: [...state.cachedViews]
        //     })
        // })
        // },
        delCachedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_CACHED_VIEW', view)
            resolve([...state.cachedViews])
            // console.log(state.cachedViews)
        })
        },
        // delAllCachedViews({ commit, state }) {
        //     return new Promise(resolve => {
        //       commit('DEL_ALL_CACHED_VIEWS')
        //       resolve([...state.cachedViews])
        //     })
        //   },
        delVisitedViews({commit, state}, view) {
            return new Promise((resolve) => {
                commit('DEL_VISITED_VIEWS', view)
                commit('DEL_CACHED_VIEW', view)
                resolve({
                    visitedViews: [...state.visitedViews],
                    cachedViews: [...state.cachedViews]
                })
            })
        },
        delOthersViews({commit, state}, view) {
            return new Promise((resolve) => {
                commit('DEL_OTHERS_VIEWS', view)
                resolve({
                    visitedViews: [...state.visitedViews],
                    cachedViews: [...state.cachedViews]
                })
            })
        },
        delAllViews({commit, state}) {
            return new Promise((resolve) => {
                commit('DEL_ALL_VIEWS')
                resolve([...state.visitedViews])
            })
        }
    }
}

export default tagsView
